package com.ermace.robo.jumper.game.Player;

import java.util.List;

import android.content.Context;

import com.ermace.robo.jumper.game.Map;
import com.ermace.robo.jumper.game.Sound;
import com.ermace.robo.jumper.game.Kill.Combo;
import com.ermace.robo.jumper.game.Kill.KillSpree;

public class Player extends Robot{

	private static final double COMBO_TIME = 5.0;
	
	private int score = 0;
	private int numberOfKills = 0;
	
	private Combo combo= Combo.NONE;
	private KillSpree spree = KillSpree.NONE;
	private double comboTime = 0;
	
	private int comboMulti = 1;

	
	public Player(double gravity, double moveSpeed, Position position, double jumpSpeed,
			Map map, List<Robot> robots, RobotColour colour, Context context) {
		super(gravity, moveSpeed, position, jumpSpeed, map, robots, colour, context);
	}
	
	/**
	 * @return respawnTimer 
	 */
	public double getRespawnTime()
	{
		return timeDeath;
	}
	
	/**
	 * @return comboTime 
	 */
	public double getComboTime()
	{
		return comboTime;
	}
		
	/**
	 * @return comboTime 
	 */
	public int getComboMultiplicator()
	{
		//return combo.getComboMultiplier();
		return comboMulti;
	}
	
	/**
	 * @return lives 
	 */
	public int getLives()
	{
		return lives;
	}
	
	@Override
	protected double getKillScore() {
		// TODO Score berekenen aan de hand van de spree en combo ...
		return 1;
	}
	
	@Override
	public void kill() {
		super.kill();
//		reduceLives();
		if (state == State.DEATH)
			lives--;
	}

	@Override
	public void updatePosition(double elapsedTimeMS) {
		super.updatePosition(elapsedTimeMS);
		
		updateComboTime(elapsedTimeMS);
	}
	
	private void updateComboTime(double elapsedTimeMS)
	{
		if(comboTime > 0)
		{
			comboTime -= elapsedTimeMS / 1000;
		}
		else{
			combo = Combo.NONE;
			comboMulti = 1;
		}
	}
	
	public int getScore()
	{
		return score;
	}
	
//	/**
//	 * recude the number of lives 
//	 */
//	private void reduceLives() // TODO kan weg
//	{
//		lives -= 1;
//	}
	
	/**
	 * 
	 */
	private void updateCombo() {
		comboMulti += 1;
		
		
		combo = Combo.getByOrdinal(combo.ordinal() + 1);
//		if (combo != Combo.NONE && combo != Combo.SINGLE_KILL) {
		if (combo.ordinal() > 1) {
			//TODO
			//play sound
			Sound.getInstance(context).comboSound(combo);
			//show text
			score += combo.getScore();
		}

		comboTime = COMBO_TIME;
	}
	
	private void updateSpree() {
		KillSpree next = KillSpree.getSpreeByKills(++numberOfKills);
		if (next != spree) {
			//TODO
			//play sound
			Sound.getInstance(context).killSpreeSound(next);
			//show text
			score += next.getScore();
		}
		
		spree = next;
	}

	@Override
	protected void kill(Robot toKill) {
//		if (toKill.state == State.ALIVE) {
//			toKill.killed();
//			score += toKill.getKillScore() * combo.getComboMultiplier();
//			updateCombo();
//			updateSpree();
//			Sound.getInstance(context).killSound();
//		}
//
//		jumpWhenKill();
		
		super.kill(toKill);
		
		if (toKill.timeDeath == RESPAWN_TIME) {		// We just killed him
			updateCombo();
			updateSpree();
			score += toKill.getKillScore() * combo.getComboMultiplier();
		}
	}
	
	@Override
	protected void resetState()
	{
		super.resetState();
		combo = Combo.NONE;
		spree = KillSpree.NONE;
		numberOfKills = 0;
		comboTime = 0;
		comboMulti = 1;
	}
	
	public void incScore()
	{
		score += 1;
	}
}
